﻿using System;
using System.Windows.Forms;
using ImageEncryptor.Core.Security;
using ImageEncryptor.Validators;
using System.IO;
using ImageEncryptor.Properties;
using System.Drawing;

namespace ImageEncryptor.Presentation
{
    #region MainForm
    public partial class MainForm : Form
    {
        #region Private fields

        private string _fileNameExtracted;
        private byte[] _dataDecrypted;

        #endregion

        #region Constructors

        public MainForm()
        {
            InitializeComponent();
            RegisterEventHandlers();
            SetStandByStatus();
        }

        #endregion

        #region Event handlers

        void btnReset_Click(object sender, EventArgs e)
        {
            _fileNameExtracted = string.Empty;
            _dataDecrypted = null;
            txtCarrierBitmapFilePath.Text = string.Empty;
            txtHostedBitmapFilePath.Text = string.Empty;
            txtKey.Text = string.Empty;

            SetStandByStatus();
        }

        void btnGetKey_Click(object sender, EventArgs e)
        {
            txtKey.Text = Encryptor.GetDistributableKey();
        }

        void btnDecrypt_Click(object sender, EventArgs e)
        {
            try
            {
                SetWorkingStatus();

                using(var decryptor = new Decryptor(txtCarrierBitmapFilePath.Text, txtKey.Text))
                {
                    decryptor.DoJob(true);
                    _fileNameExtracted = decryptor.FileNameExtracted;
                    _dataDecrypted = decryptor.DataExtracted;
                }
                string filePath = GetPathToSaveFile(_fileNameExtracted);
                if (filePath != null)
                    File.WriteAllBytes(filePath, _dataDecrypted);

                SetSuccessStatus();
            }
            catch (Exception ex)
            {
                SetErrorStatus();
                MessageBox.Show(ex.Message);
            }
        }

        void btnEncrypt_Click(object sender, EventArgs e)
        {
            try
            {
                SetWorkingStatus();

                using (var encryptor = new Encryptor(txtCarrierBitmapFilePath.Text,
                   txtHostedBitmapFilePath.Text, txtKey.Text))
                    encryptor.DoJob();

                SetSuccessStatus();
            }
            catch (Exception ex)
            {
                SetErrorStatus();
                MessageBox.Show(ex.Message);
            }
        }

        void btnPickUpHostedBitmap_Click(object sender, EventArgs e)
        {
            string bitmapPath = this.GetBitmapPathFromDialog();
            try
            {
                if (bitmapPath != null)
                    txtHostedBitmapFilePath.Text = bitmapPath;
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.InnerException.Source,"Bad File: " + ex.Message);
            }
        }

        void btnPickUpCarrierBitmap_Click(object sender, EventArgs e)
        {
            string bitmapPath = this.GetBitmapPathFromDialog();
            try
            {
                if (bitmapPath != null && BitmapValidator.Validate(bitmapPath))
                    txtCarrierBitmapFilePath.Text = bitmapPath;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.StackTrace, "Bad bitmap: " + ex.Message);
            }
        }

        #endregion

        #region Private methods

        private string GetBitmapPathFromDialog()
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Filter = "Image file (*.jpg;*.jpeg;*.bmp;*.gif;*.png)|*.jpg;*.jpeg;*.bmp;*.gif;*.png|All files|*.*";

            if (openFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                return openFileDialog.FileName;
            }

            return null;
        }

        private string GetPathToSaveFile(string initialFileName)
        {
            SaveFileDialog saveFileDialog = new SaveFileDialog();
            saveFileDialog.Filter = "All files|*.*";
            saveFileDialog.FileName = initialFileName;

            if (saveFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                return saveFileDialog.FileName;

            return null;
        }

        private void RegisterEventHandlers()
        {
            btnReset.Click+=new EventHandler(btnReset_Click);
            btnGetKey.Click+=new EventHandler(btnGetKey_Click);
            btnDecrypt.Click+=new EventHandler(btnDecrypt_Click);
            btnEncrypt.Click+=new EventHandler(btnEncrypt_Click);
            btnPickUpHostedBitmap.Click+=new EventHandler(btnPickUpHostedBitmap_Click);
            btnPickUpCarrierBitmap.Click+=new EventHandler(btnPickUpCarrierBitmap_Click);
        }

        private void SetWorkingStatus()
        {
            pctStatus.Image = Resources.Working;
            lblStatus.Text = "Working...wait a second...";
            lblStatus.Refresh();
            pctStatus.Refresh();
        }

        private void SetErrorStatus()
        {
            pctStatus.Image = Resources.Ball_Error;
            lblStatus.Text = "Last operation failed!";
            lblStatus.Refresh();
            pctStatus.Refresh();
        }

        private void SetStandByStatus()
        {
            pctStatus.Image = Resources.Ball_Standby;
            lblStatus.Text = "Stand by mode...Zzzzz...";
            lblStatus.Refresh();
            pctStatus.Refresh();
        }

        private void SetSuccessStatus()
        {
            pctStatus.Image = Resources.Ball_Ok;
            lblStatus.Text = "Task was completed successfully!";
            lblStatus.Refresh();
            pctStatus.Refresh();
        }
        #endregion
    }
    #endregion
}
